package com.inhim.pjhim.adapter.provider;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chaychan.adapter.BaseItemProvider;
import com.inhim.pjhim.R;
import com.inhim.pjhim.activity.AlllCateActivity;
import com.inhim.pjhim.activity.ChapterActivity;
import com.inhim.pjhim.activity.TypeListActivity;
import com.inhim.pjhim.adapter.ChapterItemGridviewAdapter;
import com.inhim.pjhim.adapter.ReadingTwoAdapter;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.view.MyGridView;

/**
 * @author ChayChan
 * @description: 章节标题
 * @date 2018/3/22  14:48
 */

public class ChapterItemProvider extends BaseItemProvider<ReaderTypeList, BaseViewHolder> {

    private Context context;
    private String TAG;
    public ChapterItemProvider(Context context,String TAG) {
        this.context=context;
        this.TAG=TAG;
    }

    @Override
    public void convert(BaseViewHolder helper, final ReaderTypeList news, int i) {
        if (news == null) {
            //如果没有，则直接跳过
            return;
        }
        try{
            //处理相关业务逻辑
            MyGridView recyclerView = helper.getView(R.id.recyclerView);
            ChapterItemGridviewAdapter adapter = new ChapterItemGridviewAdapter(news,context);
            recyclerView.setAdapter(adapter);
            recyclerView.setOnItemClickListener((parent, view, position, id) -> {
                if(position!=7){
                    Intent intent=new Intent(context, TypeListActivity.class);
                    intent.putExtra("TAG",TAG);
                    intent.putExtra("ReaderType",news.getPage().getList().get(position));
                    intent.putExtra("TAG","ReaderItemProvider");
                    if(news.getPage().getList().get(position).getType()==1){
                        intent.putExtra("Title","阅读");
                    }else if(news.getPage().getList().get(position).getType()==2){
                        intent.putExtra("Title","视频");
                    }else if(news.getPage().getList().get(position).getType()==3){
                        intent.putExtra("Title","聆听");
                    }
                    intent.putExtra("ReaderTypeId",news.getPage().getList().get(position).getReaderTypeId());
                    intent.putExtra("position",position);
                    intent.putExtra("ParentId",news.getPage().getList().get(position).getParentId());
                    intent.putExtra("Type",String.valueOf(news.getPage().getList().get(position).getType()));
                    context.startActivity(intent);
                }else{
                    Intent intent=new Intent(context, AlllCateActivity.class);
                    context.startActivity(intent);
                }

            });
            TextView tv_lookall = helper.getView(R.id.tv_lookall);
            TextView tv_title = helper.getView(R.id.tv_title);
            RelativeLayout rela1=helper.getView(R.id.rela1);
            rela1.setVisibility(View.GONE);
            switch (news.getPage().getList().get(0).getType()){
                case 1:
                    tv_title.setText("阅读分类");
                    break;
                case 2:
                    tv_title.setText("视频分类");
                    break;
                case 3:
                    tv_title.setText("音频分类");
                    break;
            }

            tv_lookall.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, ChapterActivity.class);
                intent.putExtra("type",String.valueOf(news.getPage().getList().get(0).getType()));
                mContext.startActivity(intent);
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public int viewType() {
        return ReadingTwoAdapter.CHAPTER_ITEM;
    }

    @Override
    public int layout() {
        return R.layout.item_chapter_provider;
    }
}
